<?php
class Model_Dbtable_Wowspell extends  Zend_Db_Table_Abstract{

	protected $_name = 'db_wow_spell';
	
	
	//详细
	public function showitem($id){
		$table = $this->getAdapter();
		$where = $table->quoteInto('id = ?', "$id");
		
		$results = $this->fetchRow($where);

		return $results;
	}
	
	
	// 列表
	public function showlist($class, $page, $purl="", $where="1=1"){
		$multipage = new Model_Core_Multipage();
		
		$table = $this->getAdapter();
		
		if ($class != "-1"){
			$where .= $table->quoteInto(' AND classid = ?', "$class");
			$purl  .= "?class=$class";
		}
		
		

		$sql = "SELECT count(*) as num FROM $this->_name WHERE $where";
		$howpage = $table->query($sql)->fetchObject()->num;

		$count = "20";
		$howpage = ceil($howpage / $count);
		$offset	 = $page * $count - $count;
		

		$multipage = $multipage->html_multi($page, $howpage, $purl);
		
		$query = $table->query(  
				"SELECT id, name, icon, classid  
				FROM $this->_name 
				WHERE $where 
				ORDER BY id 
				LIMIT $offset, $count
			");  

		$menuclass = new Model_Dbtable_Wowmenuclass();
		
		$results = array();
		$i = 0;
		while ($value = $query->fetch()) {  
		    $results[]					= $value;
		    $results[$i][id]		= $value[id];
		    $results[$i][name]		= $value[name];
		    $results[$i][icon]		= $value[icon];
			$classname	= $menuclass->classname($value[classid]);
			$results[$i][class_name]	= $classname[class_name];
		    $i++;
		}  
		
		$row = array(
			"results" 	=> $results,
			"multipage"	=> $multipage
		);
		
		return $row;
	}


	public function ajaxsearch($query)
	{
		$table = $this->getAdapter();

		$sql = "SELECT name FROM $this->_name WHERE name LIKE '%$query%' OR id LIKE '%$query%' LIMIT 8";
		$query = $table->query($sql);
		

    	$results = array();
		$i = 0;
		while ($value = $query->fetch()) {  
			 $results[$i] = $value[name];
			$i++;
		}
		return $results;
	}
}